package com.bbcare.followup.plat.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.comm.ToolUtils;
import com.bbcare.followup.plat.entity.Tguidance;
import com.bbcare.followup.plat.store.ibatis.IPatientDAO;
import com.bbcare.plat.support.service.IBizInterAsClient;



public class PtPushPatientsSend{
	private final Log logger = LogFactory.getLog(PtPushPatientsSend.class);

	@Autowired
	private IBizInterAsClient bizInterClient;
	@Autowired
	private IPatientDAO patientDao;
	public void executeTask() {
		// 为了排序使用LinkedHashMap
		Map<String, Object> retMap = new LinkedHashMap<String, Object>();
		retMap.put("pushcode", Constants.SOURCE_ACCESS_PUCHCODE);
		retMap.put("platform_code", Constants.SOURCE_HOSPTAL_CODE);
		retMap.put("platform_access", Constants.SOURCE_ACCESS_CODE);
		retMap.put("code", Constants.STRING_SERVICECODE_A09U004O);
		try {
			callHispital(retMap,3);
		} catch (Exception e) {
			logger.info(e);
		}
		logger.warn("puchcode:结束。。。。。");
	     return;
	}


	// 查询用户信息和队列实例信息
	private List<Map<String, Object>> getUserQueueInfo(Map<String, Object> retMap) throws Exception {
		retMap.put("userStatus", "1");
		List<Map<String, Object>> listone = patientDao.listPtArchInfoByCondition(retMap);
		if (CollectionUtils.isEmpty(listone)) {
			logger.info("未查询当前患者信息！");
		}
		return listone;
	}

	//处理数据
	private void dealWith(List<Map<String, Object>> list) throws Exception {
		String userId ="";
		String tenantId = "shgjhpfyb01";
		Map<String,Object> mapIn = new HashMap<String,Object>();
		mapIn.put("tenantId", tenantId);
		if(CollectionUtils.isEmpty(list)) {
			logger.info("没有患者推送信息！");
		}else {
			for(int i=0;i<list.size();i++) {
				Map<String, Object> map = list.get(i);
				if(!StringUtils.isBlank((String)map.get("platform_key"))) {
					userId = (String)map.get("platform_key");
					mapIn.put("userId", userId);
					// 查询用户和队列实例信息
					List<Map<String, Object>> lists = getUserQueueInfo(mapIn);
					if(CollectionUtils.isEmpty(lists)) {
						continue;
					}
					String partsh = (String) lists.get(0).get("pathash");// 病人识别码
					String patName = (String) lists.get(0).get("userName");// 用户姓名
					if(StringUtils.isBlank(partsh)||StringUtils.isBlank(patName)) {
						continue;
					}
					// 拼接入参
					mapIn.put("patName", patName);
					mapIn.put("pathash", partsh);
					mapIn.put("platform_code", Constants.SOURCE_HOSPTAL_CODE);
					mapIn.put("platform_key", userId);
					mapIn.put("code", Constants.STRING_SERVICECODE_A09U005O);
					mapIn.remove("userId");
					mapIn.remove("tenantId");
					mapIn.remove("userStatus");
					GetInstruction(mapIn);
				}
			}
		}

	}

	private void GetInstruction(Map<String, Object> mapIn) throws Exception {
		List<Map<String, Object>> list = callHispital(mapIn, 1);
		if(CollectionUtils.isEmpty(list)) {
			logger.info("没有患者推送指导意见列表！");
		}else {
			GetInstructionInfomation(list,mapIn);
		}
	}

	private void GetInstructionInfomation(List<Map<String, Object>> list,Map<String, Object> param) throws Exception {
		String tenantId = "shgjhpfyb01";
		Map<String, Object> mapIn = new HashMap<String,Object>();
		String  optime = DateUtil.getDateA(new Date());
		List<Tguidance> tgList = new ArrayList<Tguidance>();
		if(CollectionUtils.isEmpty(list)) {
			logger.info("没有患者推送指导意见列表！");
		}else {
			for(int i=0;i<list.size();i++) {
				Map<String,Object> retMap = list.get(i);
				// 拼接入参
				mapIn.put("yjid", retMap.get("yjid"));
				mapIn.put("pathash", (String) param.get("pathash"));
				mapIn.put("platform_code",  Constants.SOURCE_HOSPTAL_CODE);
				mapIn.put("platform_key", param.get("platform_key"));
				mapIn.put("code", Constants.STRING_SERVICECODE_A09U006O);
				// 远程调用医院接口
				List<Map<String, Object>> listone = callHispital(mapIn, 2);
				if(!CollectionUtils.isEmpty(listone)) {
					//指导意见详情
					for(int j=0;j<listone.size();j++) {
						Map<String,Object> retMap1 = listone.get(j);
						Tguidance tg =  new Tguidance();
						tg.setCl((String)retMap1.get("cl"));
						tg.setCreateTime(optime);
						tg.setDoctor((String)retMap1.get("doctor"));
						tg.setFs((String)retMap1.get("fs"));
						tg.setGys((String)retMap1.get("gys"));
						tg.setId(SeqUtil.getSeqNo("98"));
						tg.setJynl((String)retMap1.get("jynl"));
						tg.setJzrq((String)retMap.get("jzrq"));
						tg.setQt((String)retMap1.get("qt"));
						tg.setTenantId(tenantId);
						tg.setUpdateTime(optime);
						tg.setUserId((String)param.get("platform_key"));
						tg.setWssd((String)retMap1.get("wssd"));
						tg.setYjdate((String)retMap1.get("yjdate"));
						tg.setYjid((String)retMap.get("yjid"));
						tg.setZd((String)retMap1.get("zd"));
						tg.setPathash((String) param.get("pathash"));
						tgList.add(tg);
					}
					patientDao.savetGuidance(tgList);
				}else {
					logger.info("没有患者推送指导意见详情！");
				}
			}
		}
	}

	// 调用医院接口和处理医院接口返回信息通用
	@Transactional
	private List<Map<String, Object>> callHispital(Map<String, Object> retMap, int i) throws Exception {
		try {
		String argmsg = "";// 调用医院接口返回的信息
		argmsg = bizInterClient.call(Constants.SERVICE_NAME_FOR_SERVICE, "", retMap);
		logger.info("调用医院接口出参患者清单：" + argmsg);
		List<Map<String, Object>> listone = ToolUtils.FormatXml(argmsg);
		if(i==3) {
			//处理数据
			this.dealWith(listone);
			String updateTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 当前时间
			String updateAccept = SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID);// 操作流水
			Map<String,Object> mapInfo = new HashMap<String,Object>();
			Date date = new Date();
			String dateTime = DateUtil.getDate_8(date);
			mapInfo.put("id", updateAccept);
			mapInfo.put("templ_name", "医院推送患者列表");
			mapInfo.put("create_time", updateTime);
			mapInfo.put("template", argmsg);
			mapInfo.put("bak1", dateTime);
			mapInfo.put("bak2", Constants.STRING_SERVICECODE_A09U004O);
			mapInfo.put("bak3", "");
			patientDao.insertIntooutTableInfo(mapInfo);
		}
			logger.info("转化后的list：" + listone.toString());
			return listone;
		} catch (Exception e) {
			logger.info(e);
		}
		return null;
	}



}
